package com.gtc.zsk.manager;

import com.gtc.zsk.model.entity.User;
import com.gtc.zsk.model.vo.UserAccountAndAccessNum;
import com.gtc.zsk.service.UserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class MailManager {

    @Value("${spring.mail.username}")
    private String from;

    @Resource
    private JavaMailSender sender;

    @Resource
    private UserService userService;

    @Resource
    private RedisTemplate redisTemplate;


    public void sendMessage(String subject, String content, String to){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setSubject(subject);
        message.setText(content);
        message.setTo(to);
        sender.send(message);
    }

    public void dailySendMessage(){
        HashOperations ops = redisTemplate.opsForHash();
        Map<String, String> redisMap = ops.entries("operations");
        SimpleMailMessage message = new SimpleMailMessage();
        LocalDate now = LocalDate.now();
        int day = now.getDayOfMonth();
        int month = now.getMonthValue();
        String subject = String.format("%s月%s日浏览情况报告", month, day);
        message.setFrom(from);
        message.setSubject(subject);
        message.setText(transformToContent(redisMap));
        message.setTo(from);
        sender.send(message);
        Map<String, String> map = ops.entries("operations");
        if (map.size() > 0) {
            ArrayList<String> keys = new ArrayList<>();
            for (String key : map.keySet()){
                keys.add(key);
            }
            ops.delete("operations", keys.toArray());
        }
    }

    public String transformToContent(Map<String, String> map){
        //   {0:0:0:0:0:0:0:1=5,    1871810916349243394=0:0:0:0:0:0:0:1}
        StringBuilder result = new StringBuilder();;
        HashMap<String, UserAccountAndAccessNum> resultMap = new HashMap<>();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            //如果这个entry是 ip-num
            if (key.contains(":") || key.contains(".")){
                UserAccountAndAccessNum userAccountAndAccessNum = new UserAccountAndAccessNum();
                userAccountAndAccessNum.setAccessNum(value);
                resultMap.put(key, userAccountAndAccessNum);
            }else {//如果这个entry是 id-ip
                User user = userService.getById(key);
                UserAccountAndAccessNum userAccountAndAccessNum = resultMap.get(value);
                userAccountAndAccessNum.setUserAccount(user.getUserAccount());
            }
        }
        for (Map.Entry<String, UserAccountAndAccessNum> entry : resultMap.entrySet()){
            String key = entry.getKey();
            UserAccountAndAccessNum userAccountAndAccessNum = entry.getValue();
            String userAccount = userAccountAndAccessNum.getUserAccount();
            String accessNum = userAccountAndAccessNum.getAccessNum();
            if (userAccount != null){
                result.append(userAccount).append("      ").append("/").append("      ").append(key).append("      ").append("/").append("      ").append(accessNum).append("次").append("\n");
            }else
                result.append("未登录").append("      ").append("/").append("      ").append(key).append("      ").append("/").append("      ").append(accessNum).append("次").append("\n");
        }
        return result.toString();
    }
}
